package com.example.bbook.services.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.bbook.entity.GoodsEntity;
import com.example.bbook.entity.SpeciesEntity;
import com.example.bbook.mapper.SpeciesMapper;
import com.example.bbook.mapper.UserMapper;
import com.example.bbook.services.CRUDServiceInterface;
import com.example.bbook.util.HashMapTool;
import com.example.bbook.util.PageTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

@Service
public class GoodsServiceImpl implements CRUDServiceInterface<GoodsEntity> {
    @Autowired
    private BaseMapper<GoodsEntity> baseMapper;

    @Autowired
    private SpeciesMapper speciesMapper;

    @Autowired
    private UserMapper userMapper;

    @Override
    public HashMap<String, Object> findAll(PageTool pageConfig, GoodsEntity data) {
        HashMapTool hashMapTool = new HashMapTool();
        Integer page = 1, pageSize = 10;
        if (pageConfig.normal()) {
            page = pageConfig.getPage();
            pageSize = pageConfig.getPageSize();
        }
        IPage<GoodsEntity> userPage = new Page<>(page, pageSize);
        QueryWrapper<GoodsEntity> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.orderByDesc("id");
        if (data.getUserId() != null && data.getUserId() > 0) {
            userQueryWrapper.eq("user_id", data.getUserId());
        }

        if (data.getId() != null && data.getId() > 0) {
            userQueryWrapper.eq("id", data.getId());
        }

        if (data.getSpeciesId() != null) {
            userQueryWrapper.eq("species_id", data.getSpeciesId());
        }

        if (data.getName() != null) {
            userQueryWrapper.like("name", data.getName());
        }

        if (data.getIsDel() != null) {
            userQueryWrapper.eq("is_del", data.getIsDel());
        }


        ArrayList<HashMap<String, Object>> goodsTypeArr = new ArrayList<>();
        ArrayList<HashMap<String, Object>> goodsUserArr = new ArrayList<>();
        ArrayList<HashMap<String, Object>> goodsStatusArr = new ArrayList<>();

        QueryWrapper<SpeciesEntity> speciesEntityQueryWrapperGoodsType = new QueryWrapper<>();
        QueryWrapper<SpeciesEntity> speciesEntityQueryWrapperGoodsStatus = new QueryWrapper<>();
        speciesEntityQueryWrapperGoodsType.eq("dict_type", "goods");
        speciesEntityQueryWrapperGoodsType.notIn("parent_id", 0);

        speciesEntityQueryWrapperGoodsStatus.eq("dict_type", "goods_status");
        speciesEntityQueryWrapperGoodsStatus.eq("parent_id", 0);

        speciesMapper.selectList(speciesEntityQueryWrapperGoodsType).forEach(item -> {
            goodsTypeArr.add(hashMapTool.getListKeyValue(item.getName(), item.getId(), item.getTitle()));
        });

        speciesMapper.selectList(speciesEntityQueryWrapperGoodsStatus).forEach(item -> {
            goodsStatusArr.add(hashMapTool.getListKeyValue(item.getName(), item.getId(), item.getTitle()));
        });


        userMapper.selectList(null).forEach(item -> {
            goodsUserArr.add(hashMapTool.getListKeyValue(item.getPassword(), item.getId(), item.getUsername()));
        });


        GoodsEntity goodsEntity = new GoodsEntity();
        goodsEntity.push("speciesId", goodsTypeArr, "商品类型", new int[]{1, 1, 1, 1}, new String[]{"species", "title"})
                .push("userId", goodsUserArr, "用户", new int[]{1, 1, 1, 1}, new String[]{"user", "username"})
                .push("isDel", goodsStatusArr, "商品状态", new int[]{1, 1, 1, 1}, new String[]{"goodsStatus", "title"});


        userPage = baseMapper.selectPage(userPage, userQueryWrapper);

        List<GoodsEntity> goodsEntityList = userPage.getRecords();
        goodsEntityList.forEach((item) -> {
            item.setSpecies(speciesMapper.selectById(item.getSpeciesId()));
            item.setGoodsStatus(speciesMapper.selectById(item.getIsDel()));
            item.setUser(userMapper.selectById(item.getUserId()));
        });

        HashMap<String, Object> resMap = new HashMap<>();
        resMap.put("recode", userPage.getRecords());
        resMap.put("columns", goodsEntity.thisColumns());
        resMap.put("total", baseMapper.selectCount(userQueryWrapper));
        return resMap;
    }

    @Override
    public Boolean add(GoodsEntity data) {
        return baseMapper.insert(data) > 0;
    }

    @Override
    public Boolean deleteByIds(Integer[] ids) {
        return baseMapper.deleteBatchIds(Arrays.asList(ids)) == ids.length;
    }

    @Override
    public Boolean updateById(GoodsEntity data) {
        return baseMapper.updateById(data) == 1;
    }
}
